package sk.tuke.client.areas;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuBar;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.MenuItemSeparator;
import sk.tuke.client.components.buttons.ApplicationMenuButton;
import sk.tuke.client.interfaces.LayoutFactory;

public class ApplicationMenuArea extends HLayout {

	private static final int DEFAULT_SHADOW_DEPTH = 10;

	private static final String SEPARATOR = "separator";
	private static final String ICON_PREFIX = "icons/16/";
	private static final String ICON_SUFFIX = ".png";

	private MenuBar menuBar;
	private int menuPosition = 0;

	public ApplicationMenuArea(int height) {
		super();

		GWT.log("init ApplicationMenu()...", null);
		this.addStyleName("crm-ApplicationMenu");
		this.setHeight(height);
		menuBar = new MenuBar();
		menuBar.setHeight(height);
		this.addMember(menuBar);
	}

	public Menu addMenu(String menuName, int width, String menuItemNames) {
		Menu menu = new Menu();
		menu.setTitle(menuName);
		menu.setShowShadow(true);
		menu.setShadowDepth(DEFAULT_SHADOW_DEPTH);
		menu.setWidth(width);

		String[] menuItems = process(menuItemNames);

		for (int i = 0; i < menuItems.length; i++) {
			String menuItemName = menuItems[i].replaceAll("\\W", "");

			if (menuItemName.contentEquals(SEPARATOR)) {
				MenuItemSeparator separator = new MenuItemSeparator();
				menu.addItem(separator);
				continue;
			}

			MenuItem menuItem = new MenuItem(menuItems[i], getIcon(menuItems[i]));
			menu.addItem(menuItem);
		}

		Menu[] menus = new Menu[1];
		menus[0] = menu;
		menuBar.addMenus(menus, menuPosition);
		menuPosition++;

		return menus[0];
	}

	public Menu addMenu(String menuName, int width) {
		Menu menu = new Menu();
		menu.setTitle(menuName);
		menu.setShowShadow(true);
		menu.setShadowDepth(DEFAULT_SHADOW_DEPTH);
		menu.setWidth(width);

		Menu[] menus = new Menu[1];
		menus[0] = menu;
		menuBar.addMenus(menus, menuPosition);
		menuPosition++;

		return menu;
	}

	public void addSimpleMenu(String menuName, int width, int height, int position, LayoutFactory factory, ClickHandler clickHandler) {

		ApplicationMenuButton menuButton = new ApplicationMenuButton(menuName, factory, clickHandler);

		menuButton.setShowMenuButtonImage(false);
		menuButton.setWidth(width);
		menuButton.setHeight(height);
		menuButton.addClickHandler(clickHandler);
		this.addMember(menuButton, position);

	}

	public Menu addSubMenu(Menu parentMenu, String subMenuName, String menuItemNames) {
		Menu menu = new Menu();
		menu.setShowShadow(true);
		menu.setShadowDepth(DEFAULT_SHADOW_DEPTH);

		MenuItem menuItem = new MenuItem(subMenuName);
		String[] menuItems = process(menuItemNames);

		for (int i = 0; i < menuItems.length; i++) {
			String menuItemName = menuItems[i].replaceAll("\\W", "");

			if (menuItemName.contentEquals(SEPARATOR)) {
				MenuItemSeparator separator = new MenuItemSeparator();
				menu.addItem(separator);
				continue;
			}
			menuItem = new MenuItem(menuItems[i], getIcon(menuItems[i]));
			menu.addItem(menuItem);
		}
		menuItem = new MenuItem(subMenuName);
		parentMenu.addItem(menuItem);
		menuItem.setSubmenu(menu);

		return menu;
	}

	public final static String DELIMITER = ",";

	public static String[] process(String line) {
		return line.split(DELIMITER);
	}

	private String getIcon(String applicationName) {
		String name = applicationName.replaceAll("\\W", "");
		String icon = ICON_PREFIX + name.toLowerCase() + ICON_SUFFIX;
		return icon;
	}

}
